package com;

import com.impl.StudentDaoImpl;
import com.impl.UserDaoImpl;
import com.interfaces.IStudentDao;
import com.interfaces.IUserDao;
import com.proxies.LogInvocationHandler;

import java.lang.reflect.Proxy;

public class Main {

    public static void main(String[] args) {
        userTest();
    }

    public static void userTest() {
        Class[] proxyInterface = new Class[]{IUserDao.class, IStudentDao.class};
        LogInvocationHandler handler = new LogInvocationHandler(new UserDaoImpl());
        ClassLoader classLoader = Main.class.getClassLoader();
        IUserDao proxyBusiness = (IUserDao) Proxy.newProxyInstance(classLoader, proxyInterface, handler);
        System.out.println(proxyBusiness.getById(1111).getId());
        System.out.println(proxyBusiness.getById(1111).getName());
    }

    public static void studentTest() {
        Class[] proxyInterface = new Class[]{IUserDao.class, IStudentDao.class};
        LogInvocationHandler handler = new LogInvocationHandler(new StudentDaoImpl());
        //生成代理类的字节码加载器
        ClassLoader classLoader = Main.class.getClassLoader();
        //织入器，织入代码并生成代理类
        IStudentDao proxyBusiness = (IStudentDao) Proxy.newProxyInstance(classLoader, proxyInterface, handler);
        System.out.println(proxyBusiness.getById(1111).getNo());
        System.out.println(proxyBusiness.getById(1111).getScore());
    }
}
